let mediaStream

export function getMedia(constraints, video) {
  // 获得video摄像头区域
  const promise = navigator.mediaDevices.getUserMedia(constraints)
  promise.then(function(MediaStream) {
    mediaStream = MediaStream
    video.srcObject = MediaStream
    video.play()
  })
}

export function stop() {
  mediaStream.getTracks()[0].stop()
}

export function takePhoto(type, quality, video, canvas, width, height) {
  // 获得Canvas对象
  const ctx = canvas.getContext('2d')
  // console.log('c:'+video.offsetWidth)
  // console.log('c:'+video.offsetHeight)
  canvas.width = width
  canvas.height = height
  ctx.drawImage(video, 0, 0, width, height)
  // setInterval(function(){ctx.drawImage(video, 0, 0)},50)
  const base64 = canvas.toDataURL('image/' + type, quality)
  return { base64, width, height }
}
